﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive.Concurrency;
using Wulong.Phone.Dianping.Models;
using Wulong.Phone.Dianping.Utils;
using Wulong.Phone.Dianping.i18n;

namespace Wulong.Phone.Dianping.Services
{
    public class ChannelService
    {
        public static IObservable<Channel> ObservableChannel(string cityCode)
        {
            return
                new AnonymousObservable<Channel>(
                    observer => Scheduler.ThreadPool.Schedule(
                        () =>
                            {
                                if (string.IsNullOrWhiteSpace(cityCode))
                                    observer.OnError(new Exception("city code is required"));
                                observer.OnNext(new Channel
                                                    {
                                                        Id = ChannelEnum.All,
                                                        Name = AppResources.Category_All,
                                                        ImageUrl = "/Images/All.png"
                                                    });
                                observer.OnNext(new Channel
                                                    {
                                                        Id = ChannelEnum.Food,
                                                        Name = AppResources.Category_Food,
                                                        ImageUrl = "/Images/Food.png"
                                                    });
                                observer.OnNext(new Channel
                                                    {
                                                        Id = ChannelEnum.Shopping,
                                                        Name = AppResources.Category_Shopping,
                                                        ImageUrl = "/Images/Shopping.png"
                                                    });
                                observer.OnNext(new Channel
                                                    {
                                                        Id = ChannelEnum.Entertainment,
                                                        Name = AppResources.Category_Entertainment,
                                                        ImageUrl = "/Images/Entertainment.png"
                                                    });
                                observer.OnNext(new Channel
                                                    {
                                                        Id = ChannelEnum.Beauty,
                                                        Name = AppResources.Category_Beauty,
                                                        ImageUrl = "/Images/Beauty.png"
                                                    });
                                observer.OnNext(new Channel
                                                    {
                                                        Id = ChannelEnum.Wedding,
                                                        Name = AppResources.Category_Wedding,
                                                        ImageUrl = "/Images/Wedding.png"
                                                    });
                                observer.OnNext(new Channel
                                                    {
                                                        Id = ChannelEnum.Kid,
                                                        Name = AppResources.Category_Kid,
                                                        ImageUrl = "/Images/Kid.png"
                                                    });
                                observer.OnNext(new Channel
                                                    {
                                                        Id = ChannelEnum.Sport,
                                                        Name = AppResources.Category_Sport,
                                                        ImageUrl = "/Images/Sport.png"
                                                    });
                                observer.OnNext(new Channel
                                                    {
                                                        Id = ChannelEnum.Hotel,
                                                        Name = AppResources.Category_Hotel,
                                                        ImageUrl = "/Images/Hotel.png"
                                                    });
                                observer.OnNext(new Channel
                                                    {
                                                        Id = ChannelEnum.Car,
                                                        Name = AppResources.Category_Car,
                                                        ImageUrl = "/Images/Car.png"
                                                    });
                                observer.OnNext(new Channel
                                                    {
                                                        Id = ChannelEnum.PublicService,
                                                        Name = AppResources.Category_PublicService,
                                                        ImageUrl = "/Images/PublicService.png"
                                                    });
                                observer.OnCompleted();
                                        
                            }));
        }

        public static Channel GetChannel(
            int channelId,
            ObservableCollection<Channel> channels)
        {
            return channels
                    .Where(x => x.Id == (ChannelEnum)channelId)
                    .FirstOrDefault();
        }
    }
}
